define(function(require, exports, module) {
  var $ = require("$");
  var bootbox = require("bootbox");
  var dtUtil = require('datatableutil');
  require("bootstrap_commonrequire");
  var loader = require("./loader/loader");
  require("moment");
  require('daterangepicker.css');
  require('bootstrap-daterangepicker');
  require('select2.css');
  require('select2');

  exports.init = function(param) {

    $(document).ready(function() {
      // 蒙版
      loader.loading("数据加载中，请稍后……");
      // 加载自定义模板-条件区域
      var template = require("./callstatus.tpl");
      var Widget = require("widget");
      var Component = Widget.extend({
        template : template
      });
      new Component({
        renderTo : "queryAreaId"
      });
      
      var status = [ {
        "id" : "INVITE",
        "text" : "发起呼叫"
      }, {
        "id" : "RINGING",
        "text" : "振铃"
      }, {
        "id" : "ANSWERED",
        "text" : "接听"
      }, {
        "id" : "CANCEL",
        "text" : "主叫侧取消呼叫"
      }, {
        "id" : "DISCONNECTED",
        "text" : "挂机"
      }, {
        "id" : "FAILED",
        "text" : "呼叫失败"
      } ];
      $('#callState').select2({
        placeholder : "请选择通话状态",
        allowClear : true,
        data : status
      });
      
      // 初始化日期范围选择控件
      var DateRange = require('daterangeinput');
      var daterangeAttrs = {
        daterangeId : "querydatewrapper_rang",
        label : "日期"
      };
      var daterangeCom = new DateRange({
        attrs : daterangeAttrs,
        renderTo : 'querydatewrapper'
      });
      daterangeCom.setRangeToToday();
      
      var service = [ {
        "id" : "TalksV2",
        "text" : "IMS多方通话"
      }, {
        "id" : "Dial",
        "text" : "IMS点击拨号"
      }, {
        "id" : "ScheduleConference",
        "text" : "专业会议"
      }, {
        "id" : "EcpDial",
        "text" : "ECP点击拨号"
      }, {
        "id" : "EcpMeet",
        "text" : "ECP多方通话"
      }, {
        "id" : "VoiceNotice",
        "text" : "IMS文字语音短信"
      }, {
        "id" : " ",// 此处有空格
        "text" : "空"
      } ];
      $('#serviceType').select2({
        placeholder : "请选择服务类型",
        allowClear : true,
        data : service
      });
      
      
      function getCondition() {
        //获取查询条件
        var sessionsId = $('#sessionsId').val();
        var calleeNbr = $('#calleeNbr').val();
        var callState = $('#callState').val();
        var serviceType = $('#serviceType').val();
        var queryStr = "";

        if (sessionsId != '' && sessionsId != null) {
          queryStr += ("call_status.vSessionsId like '%" + sessionsId + "%';");
        }
        if (calleeNbr != '' && calleeNbr != null) {
          queryStr += ("call_status.vCalleeNbr like '%" + calleeNbr + "%';");
        }
        if (callState != '' && callState != null) {
          queryStr += ("call_status.vCallState='" + callState + "';");
        }
        if (serviceType != '' && serviceType != null) {
          if (serviceType == 'TalksV2' || serviceType == 'TalksV3') {
            queryStr += ("call_status.vServiceType='" + serviceType + "' OR call_status.vServiceType='TalksV3';");
          }else{
            queryStr += ("call_status.vServiceType='" + serviceType + "';");
          }
        }
        
        // 获取时间查询参数
        var daterange = daterangeCom.getPickerRange();
        if (daterange) {
          var daterange = daterange.split("/");
          var startTime = daterange[0];
          var endTime = daterange[1];
          var startstr = startTime.replace(/-/g, "");// 除去yyyy-MM-dd中的“-”
          var endstr = endTime.replace(/-/g, "");
          
          queryStr += ("call_status.vStateTime BETWEEN '" + startstr + "000000' AND '" + endstr + "235959'" + ";");
        }
        
        return queryStr;
      }
      
      //加载表单模板和数据
      var Table = require("table");
      // 查询内容显示
      new Table({
        attrs : {
          id : "data_table",
          isHovered : false,
          isStriped : true,
          isBordered : false
        },
        renderTo : param.divWrapperId

      });
      
      // callState信息显示
      var accountinfo_data_table = $('#data_table').dataTable({
        "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "bJQueryUI" : false,
        "oLanguage" : {
          "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
        },
        "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
        'bPaginate' : true, // 是否分页
        "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
        "bServerSide" : true, // 指定服务器端分页
        "bDeferRender" : true,
        "bFilter" : true,
        "bSort" : true,
        "bInfo" : true,
        "sAjaxSource" : param.ajaxUrl,
        "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT, // 默认分页的每页记录数
        "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
          var queryparam = dtUtil.convert_aoData(aoData);

          //拼接where查询条件
          var cond = getCondition();
          if (cond != '' && cond != null) {
            queryparam.q += cond;
          }

          // 添加自己的额外参数
          oSettings.jqXHR = $.ajax({
            "dataType" : 'json',
            "contentType" : "application/json;charset=UTF-8",
            "type" : "GET",
            "url" : sSource,
            "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
            "success" : function(json) {
              if (json.flag == '000000') {
                // 隐藏蒙版
                loader.noloading();
                dtUtil.successCallback(json, aoData, fnCallback);
              } else {
                oauth2Relogin();
              }
            }
          });

        },
        "aoColumns" : [ {
          "sTitle" : "id",
          "bSortable" : false,
          "mData" : "id",
          "bVisible" : false
        }, {
          "sTitle" : "类型",
          "bSortable" : false,
          "mData" : "vType",
          "bVisible" : false
        }, {
          "sTitle" : "服务类型",
          "bSortable" : false,
          "mData" : "vServiceType",
          "mRender" : function(data, type, full) {
              if (data == "TalksV2" || data == "TalksV3")
                return "IMS多方通话";
              else if (data == "Dial")
                return "IMS点击拨号";
              else if (data == "ScheduleConference")
                return "专业会议";
              else if(data == "sms")
                return "短信";
              else if(data == "EcpDial")
                return "ECP点击拨号";
              else if(data == "EcpMeet")
                return "ECP多方通话";
              else if(data == "VoiceNotice")
                return "IMS文字语音短信";
              else if(data == "TalksV3")
                return "文字留言";
              else if(data =="createTTSVoice")
            	return "IMS文字转语音";
              else if(data == "")
                return "空";
              else
                return data;
           }
        }, {
          "sTitle" : "会议标识",
          "bSortable" : false,
          "mData" : "vSessionsId"
        }, {
          "sTitle" : "主叫",
          "bSortable" : false,
          "mData" : "vCallerNbr"
        }, {
          "sTitle" : "被叫",
          "bSortable" : false,
          "mData" : "vCalleeNbr"
        }, {
          "sTitle" : "通话状态",
          "bSortable" : false,
          "mData" : "vCallState",
          "mRender" : function(data, type, full) {
              if (data == "INVITE")
                return "发起呼叫";
              else if (data == "RINGING")
                return "振铃";
              else if (data == "ANSWERED")
                return "接听";
              else if (data == "CANCEL")
                return "主叫侧取消呼叫";
              else if (data == "DISCONNECTED")
                return "挂机";
              else if (data == "FAILED")
                return "呼叫失败";
              else
                return "未知状态";
            }
        }, {
          "sTitle" : "状态时间",
          "bSortable" : false,
          "mData" : "vStateTime",
          "mRender" : function(data, type, full) {
              var sub = data;
              var year = sub.substring(0,4);
              var month = sub.substring(4,6);
              var day = sub.substring(6,8);
              var hour = sub.substring(8,10);
              var minute = sub.substring(10,12);
              var second = sub.substring(12,14);
              
//              var abc=a+''+b+''+c;//防止a,b,c是数字的时候变成了数字的和，中间用空字符连接
              var stateTime = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
              return stateTime;
          }
        }, {
          "sTitle" : "备注",
          "bSortable" : false,
          "mData" : "vReason"
        }],
        "fnDrawCallback" : function(oSettings) {
          // 表格绘制完成后，去除width属性，使表格可缩放
          $('#' + oSettings.sTableId).css("width", "");
        }
      });

      
      //查询按钮
      $('#querybtn').click(function() {
        //蒙版
        loader.loading("数据加载中，请稍后……");
        accountinfo_data_table.fnFilter("");
        return false;
      });

      //重置按钮
      $('#restbtn').click(function() {
        //蒙版
        loader.loading("数据加载中，请稍后……");
        $('#sessionsId').val("");
        $('#calleeNbr').val("");
        $('#callState').select2("val", "");
        $('#serviceType').select2("val", "");
        daterangeCom.setRangeToToday();
        accountinfo_data_table.fnFilter("");
        return false;
      });

    });

  };
});
